package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Customer;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.CustomerService;
import com.igeek.collateral.vo.CustomerVo;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author chh
 * @since 2024/6/27
 **/
@RestController
@RequestMapping("/admin/customer")
@Api(tags = "用户接口")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    //通过id进行查询
    @GetMapping("{id}")
    @ApiOperation("根据id查询信息")
    public Result findOne(@PathVariable("id") Long id)
    {
        try {
            Customer cs = customerService.getById(id);
            if(cs!=null) {
                return new Result(true, "查询成功", cs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"查询失败");
    }



    //分页查询
    @GetMapping("findPage")
    @ApiOperation("条件分页查询信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Customer> page = customerService.findPage(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //删除操作，根据用户id删除
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除信息")
    public Result delete (@PathVariable("id") Long id)
    {
        log.info("根据id删除信息:{}",id);
        try {
            boolean flag=customerService.delete(id);
            if(flag)
            {
                return new Result(true,"删除成功");
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }


    @PutMapping("update")
    @ApiOperation("编辑用户信息")
    public Result update(@RequestBody Customer cs){
        try {
            boolean flag = customerService.updateById(cs);
            if(flag){
                return new Result(true , "更新成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "更新失败");
    }


    @PostMapping("add")
    @ApiOperation("添加用户信息")
    public Result add(@RequestBody  Customer cs)
    {
        try {
            boolean save = customerService.save(cs);
            if(save)
            {
                return new Result(true,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");
    }








}
